package com.pdemo.core.dao;

import java.util.List;

import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.pdemo.core.entity.SystemAdmin;
import com.pdemo.core.genericdao.GenericDao;

@Repository("adminDao")
public class AdminDao extends GenericDao<SystemAdmin, Long> {

	public List<SystemAdmin> getAllSystemAdmin() {
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<SystemAdmin> cq = cb.createQuery(SystemAdmin.class);
		cq.from(SystemAdmin.class);
		Query query = entityManager.createQuery(cq);
		return query.getResultList();
	}

	public SystemAdmin getSystemAdminByMobile(String mobile) {
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<SystemAdmin> cq = cb.createQuery(SystemAdmin.class);
		Root<SystemAdmin> root = cq.from(SystemAdmin.class);
		cq.where(cb.equal(root.get("mobile"), mobile));
		Query query = entityManager.createQuery(cq);
		return (SystemAdmin) query.getSingleResult();
	}

	public void updateSystemAdmin(SystemAdmin admin) {
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaUpdate<SystemAdmin> update = cb.createCriteriaUpdate(SystemAdmin.class);
		Root e = update.from(SystemAdmin.class);
		update.set("email", admin.getEmail());
		update.where(cb.equal(e.get("id"), admin.getId()));
		Query query = entityManager.createQuery(update);
		query.executeUpdate();
	}

}
